#pragma once

#include "../xImage.h"

// CColorConverter command target

struct HSL
{
	int Hue;				// 0-359 : hue value
	double	Saturation;		// 0-1 : saturation value
	double	Luminance;		// 0-1 : luminance value

	HSL() {}
	HSL(int hue, double saturation, double luminance)
		: Hue(hue), Saturation(saturation), Luminance(luminance)
	{}
};

struct YCbCr
{
	double Y;
	double Cb;
	double Cr;

	YCbCr() {}
	YCbCr(double y, double cb, double cr)
	{
		Y = max(0.0, min(1.0, y));
		Cb = max(-0.5, min(0.5, cb));
		Cr = max(-0.5, min(0.5, cr));
	}
};

class CColorConverter : public CObject
{
public:
	CColorConverter(CxImage* pImage);

	UINT GetNearestPaletteIndex(RGBQUAD color);
	BOOL GetPaletteEntries(UINT uStartIndex, UINT cEntries, LPRGBQUAD pColors) const;

	inline void SetImage(CxImage* pImage);
	inline CxImage* GetImage();

	DWORD RGBtoDWORD(RGBQUAD rgb);
	RGBQUAD DWORDtoRGB(DWORD value);

	static HSL RGBtoHSL(RGBQUAD rgb);
	static RGBQUAD HSLtoRGB(HSL hsl);
	
	static YCbCr RGB2YCbCr(RGBQUAD rgb);
	static RGBQUAD YCbCr2RGB(YCbCr ycbcr);

private:
	void SetDefaultBitfields();
	static double Hue_2_RGB(double v1, double v2, double vH);
private:
	CxImage* m_pImage;

	DWORD m_dwBitfields[3];		//!< DIB bitfields (16 & 24 bit images)
	WORD  m_wRShift[3];			//!< Right shift values (16 & 24 bit images)
	WORD  m_wLShift[3];			//!< Left  shift values (16 & 24 bit images)
};


